<template>
  <a-modal title="登录租户选择" :width="450" :visible="tenantVisible" :closable="false" :maskClosable="false">
    <template slot="footer">
      <a-button type="primary" @click="tenantOk">确认</a-button>
    </template>

    <a-form :form="form">
      <a-form-item :labelCol="{ span: 4 }" :wrapperCol="{ span: 20 }" style="margin-bottom:10px">
        <a-tooltip placement="topLeft">
          <template slot="title">
            <span>您隶属于多租户，请选择登录租户</span>
          </template>
          <a-avatar style="backgroundColor:#87d068" icon="gold" />
        </a-tooltip>

        <a-select
          v-decorator="['tenantNo', { rules: [{ required: true, message: '请选择登录租户!' }] }]"
          placeholder="请选择登录租户"
          style="margin-left:10px;width: 80%"
        >
          <a-icon slot="suffixIcon" type="gold" />
          <a-select-option v-for="d in tenantList" :key="d.tenantNo">{{ d.tenantName }}</a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { preLogin } from '@/api/user'
export default {
  data () {
    return {
      tenantVisible: false,
      tenantList: [],
      form: this.$form.createForm(this)
    }
  },
  methods: {
    edit (account) {
      this.form.resetFields()
      this.preLogin(account)
    },
    // 根据登录名获取租户列表
    preLogin (account) {
      preLogin(account)
        .then(res => {
          const { code, data, msg } = res
          if (code === 0) {
            if (data.length === 1) {
              this.$emit('ok', data[0])
            } else {
              this.tenantList = data
              this.tenantVisible = true
            }
          } else {
            this.$message.warning(msg)
            this.$emit('error')
          }
        })
        .finally(() => {})
    },
    tenantOk () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.$emit('ok', values.tenantNo)
          this.handleCancel()
        }
      })
    },
    handleCancel () {
      this.tenantLis = []
      this.tenantVisible = false
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .ant-form-explain {
  margin-left: 42px;
}
</style>
